﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;
using System.Transactions;

namespace BusinessLogic
{
    public static class HoaDonNhap
    {
        public static Entities.HoaDonNhap[] SelectHoaDonNhap(Entities.HoaDonNhap hoadon)
        {
            Entities.HoaDonNhap[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HoaDonNhap.sp_Select_HoaDonNhap, cn);
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaHoaDonNhap, SqlDbType.VarChar).Value = hoadon.MaHoaDonNhap;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.HoaDonNhap row = new Entities.HoaDonNhap();
                    row.MaHoaDonNhap = dr[Common.Constants.HoaDonNhap.MaHoaDonNhap].ToString();
                    row.NgayNhap = DateTime.Parse(dr[Common.Constants.HoaDonNhap.NgayNhap].ToString());
                    row.MaNhaCungCap = dr[Common.Constants.HoaDonNhap.MaNhaCungCap].ToString();
                    row.TenNhaCungCap = dr[Common.Constants.HoaDonNhap.TenNhaCungCap].ToString();
                    row.NoHienThoi = dr[Common.Constants.HoaDonNhap.NoHienThoi].ToString();
                    row.NguoiGiaoHang = dr[Common.Constants.HoaDonNhap.NguoiGiaoHang].ToString();
                    row.HinhThucThanhToan = dr[Common.Constants.HoaDonNhap.HinhThucThanhToan].ToString();
                    row.MaKho = dr[Common.Constants.HoaDonNhap.MaKho].ToString();
                    row.TenKho = dr[Common.Constants.HoaDonNhap.TenKho].ToString();
                    row.HanThanhToan = DateTime.Parse(dr[Common.Constants.HoaDonNhap.HanThanhToan].ToString());
                    row.MaDonDatHang = dr[Common.Constants.HoaDonNhap.MaDonDatHang].ToString();
                    row.NgayDat = DateTime.Parse(dr[Common.Constants.HoaDonNhap.NgayDat].ToString());
                    row.MaTienTe = dr[Common.Constants.HoaDonNhap.MaTienTe].ToString();
                    row.TyGiaTienTe = dr[Common.Constants.HoaDonNhap.TyGiaTienTe].ToString();
                    row.ChietKhau = dr[Common.Constants.HoaDonNhap.ChietKhau].ToString();
                    row.ThanhToanNgay = dr[Common.Constants.HoaDonNhap.ThanhToanNgay].ToString();
                    row.ThueGTGT = dr[Common.Constants.HoaDonNhap.ThueGTGT].ToString();
                    row.TongTien = dr[Common.Constants.HoaDonNhap.TongTien].ToString();
                    row.ThanhToanSauKhiLapPhieu = dr[Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu].ToString();
                    row.MaNhanVien = dr[Common.Constants.HoaDonNhap.MaNhanVien].ToString();
                    row.TenNhanVien = dr[Common.Constants.HoaDonNhap.TenNhanVien].ToString();
                    row.GhiChu = dr[Common.Constants.HoaDonNhap.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.HoaDonNhap.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.HoaDonNhap[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.HoaDonNhap)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;
        }

        public static int InsertUpdateHoaDonNhap(Entities.HoaDonNhap hoadon)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.HoaDonNhap.sp_InsertUpdate_HoaDonNhap, cn);
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.HanhDong, SqlDbType.NVarChar).Value = hoadon.HanhDong;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaHoaDonNhap, SqlDbType.VarChar).Value = hoadon.MaHoaDonNhap;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.NgayNhap, SqlDbType.DateTime).Value = hoadon.NgayNhap;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaNhaCungCap, SqlDbType.VarChar).Value = hoadon.MaNhaCungCap;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.TenNhaCungCap, SqlDbType.NVarChar).Value = hoadon.TenNhaCungCap;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.NoHienThoi, SqlDbType.Float).Value = hoadon.NoHienThoi;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.NguoiGiaoHang, SqlDbType.NVarChar).Value = hoadon.NguoiGiaoHang;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.HinhThucThanhToan, SqlDbType.NVarChar).Value = hoadon.HinhThucThanhToan;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaKho, SqlDbType.VarChar).Value = hoadon.MaKho;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.TenKho, SqlDbType.NVarChar).Value = hoadon.TenKho;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.HanThanhToan, SqlDbType.DateTime).Value = hoadon.HanThanhToan;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaDonDatHang, SqlDbType.VarChar).Value = hoadon.MaDonDatHang;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.NgayDat, SqlDbType.DateTime).Value = hoadon.NgayDat;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaTienTe, SqlDbType.VarChar).Value = hoadon.MaTienTe;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.TyGiaTienTe, SqlDbType.Float).Value = hoadon.TyGiaTienTe;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.ChietKhau, SqlDbType.Float).Value = hoadon.ChietKhau;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.ThanhToanNgay, SqlDbType.Float).Value = hoadon.ThanhToanNgay;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.ThueGTGT, SqlDbType.Float).Value = hoadon.ThueGTGT;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.TongTien, SqlDbType.Float).Value = hoadon.TongTien;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.ThanhToanSauKhiLapPhieu, SqlDbType.Float).Value = hoadon.ThanhToanSauKhiLapPhieu;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.MaNhanVien, SqlDbType.VarChar).Value = hoadon.MaNhanVien;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.TenNhanVien, SqlDbType.NVarChar).Value = hoadon.TenNhanVien;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.GhiChu, SqlDbType.NVarChar).Value = hoadon.GhiChu;
                cmd.Parameters.Add(Common.Constants.HoaDonNhap.Deleted, SqlDbType.Bit).Value = hoadon.Deleted;
                rt = cmd.ExecuteNonQuery();
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
                if (rt != 0)
                {
                    if (hoadon.MaDonDatHang != string.Empty)
                    {
                        int t = BusinessLogic.Update.Update_TrangThaiDonDatHang("1", hoadon.MaDonDatHang, "Đã thành công");
                        if (t == 0) 
                        { return 0; }
                    }
                    int k = BusinessLogic.ChiTietHoaDonNhap.InsertUpdateListChiTietHoaDonNhap(hoadon.ChiTietHoaDon);
                    if (k == 0)
                    { return 0; }
                    int d = BusinessLogic.ChiTietKhoHang.InsertUpdateListChiTietKhoHang(hoadon.ChiTietKho);
                    if (d == 0)
                    { return 0; }
                    int u = BusinessLogic.Update.Update_DuNo("2", hoadon.MaNhaCungCap, hoadon.DuNo);
                    if (u == 0)
                    { return 0; }
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                rt = 0;
            }
            return rt;
        }
    }
}
#endregion
